<?php

namespace App\Admin\Controllers\Stat;

use App\Http\Controllers\Controller;
use App\Model\PayOrder;
use App\Model\Stat\StatDailyPayInfo;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Layout\Content;

class StatDailyPayInfoController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */

    public function index()
    {
        $this->adminId = Admin::user()->id;
        return Admin::content(function (Content $content) {
            $content->header('充值日报数据');
            $content->description('');
            $content->body($this->grid());
        });
    }

    /**
        $table->unsignedSmallInteger('pay_order_num')->default(0)->comment('充值订单数');
        $table->unsignedSmallInteger('pay_user_num')->default(0)->comment('充值用户数');
        $table->unsignedSmallInteger('pay_rmb_num')->default(0)->comment('充值总金额');
        $table->unsignedSmallInteger('wechat_order_num')->default(0)->comment('微信订单数');
        $table->unsignedSmallInteger('wechat_rmb_num')->default(0)->comment('微信充值金额');
        $table->unsignedSmallInteger('alipay_order_num')->default(0)->comment('支付宝订单数');
        $table->unsignedSmallInteger('alipay_rmb_num')->default(0)->comment('支付宝充值金额');
        $table->unsignedSmallInteger('fail_order_num')->default(0)->comment('未成功订单数');
        $table->unsignedSmallInteger('fail_user_num')->default(0)->comment('未成功充值用户数');
        $table->string('order_num_info', 200)->default('')->comment('各金额充值数');
        $table->string('user_num_info', 200)->default('')->comment('各金额充值用户数');
     */
    protected function grid()
    {
        $grid = new Grid(new StatDailyPayInfo());
        if (!request()->dt) {
            request()->merge(['dt' => ['start' => date('Y-m-d', strtotime('-7 day')), 'end' => '']]);
        }
        $grid->model()->orderBy('dt', 'desc');
        $grid->dt('日期')->sortable();
        $grid->pay_order_num('充值订单数');
        $grid->pay_user_num('充值用户数');
        $grid->pay_rmb_num('充值总金额')->display(function ($data) {
            return $data / 100;
        });
        $grid->wechat_order_num('微信订单数');
        $grid->wechat_rmb_num('微信充值金额')->display(function ($data) {
            return $data / 100;
        });
        $grid->alipay_order_num('支付宝订单数');
        $grid->alipay_rmb_num('支付宝充值金额')->display(function ($data) {
            return $data / 100;
        });
        $grid->fail_order_num('未支付成功订单数')->display(function ($data) {
            return $data;
        });
        $grid->order_num_info('各金额充值数量')->display(function ($data) {
            $orderNum = json_decode($data, true);
            ksort($orderNum);
            $str = '';
            foreach ($orderNum as $key => $num) {
                $str .= "{$key}元：{$num}<br/>";
            }
            return $str;
        });
        $grid->invite_rmb_num('邀请用户充值')->display(function ($data) {
            $percent = empty($this->pay_rmb_num) ? 0 : round($data / $this->pay_rmb_num, 3) * 100;
            return ($data / 100) . " ({$percent}%)";
        });
        $grid->uninvite_rmb_num('非邀请用户充值')->display(function ($data) {
            $percent = empty($this->pay_rmb_num) ? 0 : round($data / $this->pay_rmb_num, 3) * 100;
            return ($data / 100) . " ({$percent}%)";
        });
        $grid->invite_user_num('邀请充值用户数')->display(function ($data) {
            $percent = empty($this->pay_rmb_num) ? 0 : round($data / $this->pay_user_num, 3) * 100;
            return "{$data}({$percent}%)";
        });
        $grid->uninvite_user_num('非邀请充值用户数')->display(function ($data) {
            $percent = empty($this->pay_rmb_num) ? 0 : round($data / $this->pay_user_num, 3) * 100;
            return "{$data}({$percent}%)";
        });
        //禁用导出
        $grid->disableExport();
        //禁用行选择
        $grid->disableRowSelector();
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->between('dt', '日期')->date();
            });
        });
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableAll();
        });
        return $grid;
    }
}
